from django.db import models

# Create your models here.
from tinymce.models import HTMLField


class BookInfoManage(models.Manager):
	"""图书模型管理器类"""

	# 改变查询的结果集
	def all(self):
		# 调用父类的all，获取所有数据
		books = super().all()
		# 过滤数据
		books = books.filter(isDelete=False)
		return books

	# 封装函数 操作数据库
	def create_book(self, btitle, bpub_date):
		# 创建一个图书对象
		obj = self.model()
		obj.btitle = btitle
		obj.bpub_date = bpub_date
		# 保存到数据库
		obj.save()
		return obj


# 图书类
class BookInfo(models.Model):
	"""图书模型类"""
	# 字符串 长度为20
	btitle = models.CharField('书名', max_length=20)
	# 出版日期 日期类型
	bpub_date = models.DateField(verbose_name='出版日期')
	# 订阅量
	bread = models.IntegerField('订阅量', default=0)
	# 评论量
	bcomment = models.IntegerField('评论量', default=0)
	# 删除标记
	isDelete = models.BooleanField('是否删除', default=False)

	objects = BookInfoManage()

	def title(self):
		return self.btitle

	title.admin_order_field = 'btitle'  # 指定btitle排序
	title.short_description = '标题'  # 别名

	def __str__(self):
		# 返回书的标题
		return self.btitle


class HeroInfo(models.Model):
	"""英雄任务模型类"""
	hname = models.CharField('名称', max_length=20)  # 英雄名字
	hgender = models.BooleanField('性别', default=False)  # 英雄性别
	hcomment = models.CharField('简介', max_length=128)  # 注释
	hbook = models.ForeignKey("BookInfo", on_delete=models.CASCADE)
	# 删除标记
	isDelete = models.BooleanField('是否删除', default=False)

	def __str__(self):
		# 返回书的标题
		return self.hname


class PicTest(models.Model):
	"""上传图片"""
	goods_pic = models.ImageField(upload_to='booktest')


class AreaInfo(models.Model):
	"""省市区数据"""
	addr = models.CharField(max_length=25)
	p = models.ForeignKey("self", null=True, blank=True, on_delete=models.CASCADE)

	def p_addr(self):
		if self.p is None:
			return ''
		return self.p.addr

	def province(self):
		if self.p is None:
			return self.addr
		else:
			return ''

	def __str__(self):
		return self.addr


class GoodsInfo(models.Model):
	getcontent = HTMLField()
